package org.zhou.mapper;

import org.apache.ibatis.annotations.*;
import org.zhou.pojo.Book;
import org.zhou.pojo.Borrow;
import org.zhou.pojo.BorrowVO;
import org.zhou.pojo.Type;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Mapper
public interface BooksMapper {

    /**
     * 查询分类
     * @return
     */
    @Select("select * from type order by id desc ")
    List<Type> getClassify();

    /**
     * 通过类名查询分类
     * @param name
     * @return
     */
    @Select("select * from type where name=#{name}")
    List<Type> getByNameClassify(String name);

    /**
     * 添加分类
     * @param name
     */
    @Insert("insert into type (name) values (#{name})")
    void addClassify(String name);

    /**
     * 修改分类
     * @param id
     * @param name
     */
    @Update("update type set name=#{name} where id=#{id}")
    void updateClassify(Integer id, String name);

    /**
     * 删除分类
     * @param id
     */
    @Delete("delete from type where id=#{id}")
    void deleteClassify(Integer id);

    /**
     * 分页查询图书
     * @param bookname
     * @param type
     * @param author
     * @return
     */
    List<Book> list(String bookname, Integer type, String author, String bookId);

    /**
     * 添加图书
     * @param book
     */
    @Insert("insert into book (bookname, type, author, num, address, id_number) VALUES " +
            "(#{bookname},#{type},#{author},#{num},#{address},#{idNumber})")
    void addBook(Book book);

    /**
     * 修改图书
     * @param book
     */
    @Update("update book set bookname=#{bookname},type=#{type},author=#{author},num=#{num},address=#{address},id_number=#{idNumber} " +
            "where id=#{id}")
    void updateBook(Book book);

    /**
     * 删除图书
     * @param id
     */
    @Delete("delete from book where id=#{id}")
    void deleteBook(Integer id);

    /**
     * 分页查询借阅表
     * @param readerId
     * @param bookId
     * @param status
     * @return
     */
    List<Borrow> borrowList(String readerId, String bookId, String status);

    /**
     * 查询详情
     * @param readerId
     * @param bookId
     * @return
     */
    @Select("select r.name readerName, b.bookname," +
            "(select count(*) from borrow where reader_id=#{readerId} ) total," +
            "r.balance,r.status readerStat from book b,readers r where b.id_number=#{bookId} and r.id_number = #{readerId}")
    BorrowVO getBorrow(String readerId, String bookId);

    /**
     * 借阅
     * @param readerId
     * @param bookId
     */
    @Insert("insert into borrow ( reader_id, book_id, borrow_date, return_date) values  " +
            "(#{readerId}, #{bookId}, now(), date_add(now(), interval 1 month))")
    void addBorrow(String readerId, String bookId);

    /**
     * 还书
     * @param id
     */
    @Update("update borrow set status=0 where id=#{id}")
    void returned1(Integer id);

    /**
     * 核算余额
     * @param readerId
     * @param balance
     */
    @Update("update readers set balance=balance-#{balance} where id_number=#{readerId}")
    void returned2(String readerId, BigDecimal balance);

//    @Select("select status from readers where id_number=#{readerId}")
//    Integer getStatus(String readerId);

    /**
     * 查询归还日期
     * @param id
     * @return
     */
    @Select("select return_date from borrow where id=#{id} ")
    LocalDate getBorrowDate(Integer id);


}
